import os
import sys
import hashlib

def get_sha1sum(buffer):
    BUFFERSIZE = 128*1024
    sha1 = hashlib.sha1()
    sha1128ksum = None
    while True:
        data = buffer.read(BUFFERSIZE)
        if not data:
            break
        sha1.update(data)
        if not sha1128ksum:
            sha1128ksum = hashlib.sha1(data).hexdigest().upper()
    sha1sum = sha1.hexdigest().upper()
    return sha1sum, sha1128ksum



def create_url(filename):
    name = os.path.basename(filename)
    size = os.stat(filename).st_size
    with open(filename, "rb") as f:
        sha1sum, sha1128ksum = get_sha1sum(f)
    return "115://%s|%s|%s|%s" % (name, size, sha1sum, sha1128ksum)

def gcid_hash_file(path):
    h = hashlib.sha1()
    size = os.path.getsize(path)
    psize = 0x40000
    while size / psize > 0x200 and psize < 0x200000:
        psize = psize << 1
    with open(path, 'rb') as stream:
        data = stream.read(psize)
        while data:
            h.update(hashlib.sha1(data).digest())
            data = stream.read(psize)
    (path, filename) = os.path.split(path)

    return 'PikPak://'+filename+'|'+str(size)+'|'+h.hexdigest().upper()

def hash_f(pth:str,api:str='md5',block_size:int=1024*1024,head_size:int=256*1024)->list:
    with open(pth,'rb') as f:
        hsh=hashlib.new(api,f.read(head_size))
        head=hsh.hexdigest()
        while True:
            data=f.read(block_size)
            if not data:
                break
            hsh.update(data)
        return [hsh.hexdigest(),head,]

def _file(pth:str)->str:
    if mode == 'baidu网盘':
        _ll=[str(i) for i in hash_f(pth)+[os.path.getsize(pth),os.path.basename(pth)]]
        ans='#'.join(_ll)
        with open(mode+'_link.txt',"a+") as file:   #”w"代表着每次运行都覆盖内容
            file.write(ans+"\n")
        return os.path.abspath(pth)+' :\n'+ans+'\n'
    if mode == 'pikpak':
        ans = gcid_hash_file(pth)
        with open(mode+'_link.txt',"a+") as file:   #”w"代表着每次运行都覆盖内容
            file.write(ans+"\n")
        return os.path.abspath(pth)+' :\n'+ans+'\n'
    if mode == '115':
        ans = create_url(pth)
        with open(mode+'_link.txt',"a+") as file:   #”w"代表着每次运行都覆盖内容
            file.write(ans+"\n")
        return os.path.abspath(pth)+' :\n'+ans+'\n'

def _dir(pth:str)->str:
    ans=list()
    for r,d,f in os.walk(pth):
        for i in f:
            ans.append(_file(os.path.join(r,i)))
    return '\n'.join(ans)

def mian(pth:str)->str:
    

    if os.path.isfile(pth):
        return _file(pth)
    if os.path.isdir(pth):
        return _dir(pth)
    return '没有此文件或者目录: "'+os.path.abspath(pth)+'"\n'


sign = True  #标志位置为 True
while sign:
    
    ipt = input("1:115  2:baidu网盘 3:pikpak q:退出\n结果会保存在脚本所在目录对应txt文件\n请输入序号：")
    n = ipt
    if ipt ==  'q' or  ipt ==  'Q':
        sign = False  #标志位置为 False
        print("Thanks for playing!")
    if ipt in ['1','2','3']:
        temp_list= ['115','baidu网盘','pikpak']
        mode = temp_list[int(ipt)-1]
        if  os.path.exists(mode+'_link.txt'):    
            os.remove(mode+'_link.txt')
    while sign:

        f_ipt = input("请输入要生成"+mode+"秒传链接的文件或文件夹路径:")
        if f_ipt ==  'q' or  f_ipt ==  'Q':
            sign = False  #标志位置为 False
        else:

            结果 = mian(f_ipt)   
            print(结果)
